//
//  HMHomeCollectionViewCell.m
//  LoveFreshBeen
//
//  Created by huodian on 16/10/16.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "HMHomeCollectionViewCell.h"

#import "HMHomeFirstModel.h"

#import <UIImageView+WebCache.h>

#import "ShopCartGoodsListTool.h"
@interface HMHomeCollectionViewCell()

//@property (weak, nonatomic) IBOutlet UIImageView *hotImage;

@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UILabel *weightLable;

@property (weak, nonatomic) IBOutlet UILabel *priceLable;
@property (weak, nonatomic) IBOutlet UIButton *retainLable;

@property (weak, nonatomic) IBOutlet UIButton *reduceLable;
@property (weak, nonatomic) IBOutlet UILabel *countLable;


@end

@implementation HMHomeCollectionViewCell
- (IBAction)clickReduceBtn:(UIButton*)sender {
    
    int num = 1;
    if (sender.tag == 20)
    {
        

        if (self.model.number > [self.countLable.text integerValue]) {
            
            
         
            
              self.model.goodsCount ++;
            
            self.countLable.text = [NSString stringWithFormat:@"%ld",self.model.goodsCount];
            
            self.reduceLable.hidden = NO;
            
//            [self.model setValue:@1 forKey:@"isAdd"];
            
        }else
        {
            
            [SVProgressHUD showImage:[UIImage imageNamed:@"v2_orderSuccess"] status:@"你大爷还点,老子又不是点读机"];
            
            [SVProgressHUD setMinimumDismissTimeInterval:1];
            
            [SVProgressHUD setDefaultStyle:  SVProgressHUDStyleCustom];
            
            sender.enabled = NO;
            
            num++;
        }
        sender.enabled = YES;
       
        
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"add" object:self.model];
        



      
        self.countLable.text = [NSString stringWithFormat:@"%ld",self.model.goodsCount];
        self.reduceLable.hidden = NO;
        self.countLable.hidden = NO;
        //添加模型到单例对象的数组中
        [[ShopCartGoodsListTool sharedGoodsListTool] addGoodsToShopCartGoodsList:self.model];

  
        
    }else if(sender.tag == 10)
    {



        

        self.model.goodsCount --;
        self.countLable.text = [NSString stringWithFormat:@"%ld",self.model.goodsCount];

        
        if (self.model.goodsCount == 0) {
            
            self.reduceLable.hidden = YES;
            self.countLable.hidden = YES;
        }
        
        [[ShopCartGoodsListTool sharedGoodsListTool] reduceGoodsFromShopCartGoodsList:self.model];
        
    }
    
    
    CGRect rect = [self convertRect:self.bounds toView:[UIApplication sharedApplication].keyWindow];
    
    if (_reloadBlock&&(self.model.number+1 > [self.countLable.text integerValue])&&sender.tag == 20&& num == 1)
    {
        self.reloadBlock(rect);
        
        
    }
    if (self.model.number >= [self.countLable.text integerValue]) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:KChangeGoodCountNoti object:self.model];
        
    }
    
 

}

-(void)setModel:(HMHomeFirstModel *)model
{
    _model = model;
    
    self.nameLable.text = model.name;
    
    self.weightLable.text = model.specifics;
    
    self.priceLable.text = [NSString stringWithFormat:@"$%.1f  $%.1f",model.partner_price,model.market_price];
    
    NSMutableAttributedString* strM = [[NSMutableAttributedString alloc]initWithString:self.priceLable.text];
    
    NSRange rang = [self.priceLable.text rangeOfString:[NSString stringWithFormat:@" $%.1f",model.market_price]];
    
   //添加商品数量的判断逻辑
    self.countLable.text = [NSString stringWithFormat:@"%ld",model.goodsCount];
    self.countLable.hidden = model.goodsCount == 0 ? YES : NO;
    //添加减号按钮显示的判断
    self.reduceLable.hidden = model.goodsCount == 0 ? YES : NO;;
    
    [strM addAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSStrikethroughStyleAttributeName:@1} range:rang];
    
   
    
    self.priceLable.attributedText = strM;
    
    NSURL* url = [NSURL URLWithString:model.img];
    
    [self.hotImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"v2_placeholder_full_size"]];
    
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    // Initialization code
}

@end
